// The idea of the script is to separate the javascript logic from the view page
// This script includes common scripts used for the My Admin Page
// For design purposes, we suggest moving all javascript code into the js folder

var STATUS = {COMMITTED:1, CHANGED:0};
var DOMAIN = {PROFILE:0, ORGANIZATION:1};
var profileStatus=STATUS.COMMITTED;//the status of the user profile info editing
var orgStatus=STATUS.COMMITTED;//the status of the organization info editing

/*
 * update status after submitting the changes
 */
function updateStatus(data) {
	if (data=='profile-success' || data=='org-success' || data=='bank-success' || data=='rack-success')
	{
		$('#msg-header').show();
		$('#msg-header').html('Updated successfully.');
	}
	else if (data=='rack-error')
	{
		$('#msg-header').show();
		$('#msg-header').html('A minimum of 1 rack is required.');
	}
	else
	{
		$('#msg-header').html('Error.');
	}
	
	if (data=='profile-success')
		profileStatus=STATUS.COMMITTED;
	if (data=='org-success')  
		orgStatus=STATUS.COMMITTED;
}

/*
 * change status of domains and udpate some displaying info accordingly
 */
function changeStatus(domain, status)
{
	if(domain==DOMAIN.PROFILE)
	{
		profileStatus=status;
		if(profileStatus==STATUS.CHANGED)
		{
			document.getElementById('profile-label').innerHTML='';
			document.getElementById('user_name').innerHTML='';
		}	
	}
	else if(domain==DOMAIN.ORGANIZATION)
	{
		orgStatus=status;
		if(orgStatus==STATUS.CHANGED)
		{
			document.getElementById('org-label').innerHTML='';
			document.getElementById('organization_name').innerHTML='';
		}
	}
}

/*
 * check status of domains, confirm with the client if they want to 
 * abandon changes of the domains
 */
function checkStatus(element, url)
{
	var abandon=false;
	var message="";
	if(profileStatus==STATUS.CHANGED)
	{
		message+="Profile information has been modified\r\n";
	}
	if(orgStatus==STATUS.CHANGED)
	{
		message+="Organization Information has been modified\r\n";
	}
	if(message!="")
	{
		abandon=window.confirm(message+"Do you want to abandon these changes?");
		if(abandon)
			element.href=url;
		else
			element.href='#';
	}
}
/*
 * check the status of domains when "home" "initiate auctions" buttons are clicked
 */
function addHomeEvent()
{
	document.getElementById("ms-links4").firstChild.onclick=function(){
		checkStatus(this, baseUrl);
	}
	document.getElementById("ms-links6").firstChild.onclick=function(){
		checkStatus(this, baseUrl);
	}
}

/*
 * element: the tab link
 * tag: supposed class name of the link
 * focus on that tab link
 */
function activateTab(element, tag)
{
	var elementList=$("[name='tabs']");
	for(var i=0;i<elementList.length;i++)
	{
		if(elementList[i]==element.parentNode)//activate it
		{
			elementList[i].className=tag+(i+1)+" "+tag+"-active"+(i+1);
		}
		else
		{
			elementList[i].className=tag+(i+1);
		}
	}
}